package com.ityj.yygh.user.controller.api;

import com.ityj.yygh.model.user.Patient;
import com.ityj.yygh.result.Result;
import com.ityj.yygh.user.service.PatientService;
import com.ityj.yygh.util.AuthContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "就诊人管理接口")
@RestController
@RequestMapping(path = "/api/user/patient")
@Slf4j
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    @ApiOperation("就诊人信息查询-根据用户id")
    @GetMapping(path = "/auth/findAll")
    public Result<List<Patient>> findAllPatientInfo(HttpServletRequest request) {
        List<Patient> result = patientService.findAll(AuthContextHolder.getUserId(request));
        return Result.ok(result);
    }

    @ApiOperation("就诊人信息查询-根据就诊人Id-主键")
    @GetMapping(path = "/auth/findById/{id}")
    public Result<Patient> findById(@PathVariable("id") Long patientId) {
        Patient patient = patientService.findById(patientId);
        return Result.ok(patient);
    }

    @ApiOperation("就诊人信息新增")
    @PostMapping(path = "/auth/save")
    public Result<Object> savePatientInfo(@RequestBody Patient patient, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.savePatient(patient);
        return Result.ok();
    }

    @ApiOperation("就诊人信息删除-根据id")
    @DeleteMapping(path = "/auth/removeById/{id}")
    public Result<Object> removeById(@PathVariable("id") String id) {
        patientService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("就诊人信息修改")
    @PutMapping(path = "/auth/updateInfo")
    public Result<Object> updateInfo(@RequestBody Patient patient) {
        patientService.updatePatient(patient);
        return Result.ok();
    }

    @ApiOperation(value = "根据id获取就诊人")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        return patientService.findById(id);
    }

}
